<?php

/**
   * Connects to database server and selects database
   * @return resource (bool)
   */
function db_connect()
{

	$connection = @mysql_connect('localhost', "fmaniac_fmaniac", "fguru");

	if (!$connection)
	{
		return false;
	}

	if (!mysql_select_db('fmaniac_fitness'))
	{
		return false;
	}

	return $connection;
}


/**
	 * Selects out this weeks theme
	 * @return array
	 */
function this_weeks_theme()
{

	$cxn = db_connect();

	$query = "SELECT * FROM photos_themes order by id DESC limit 1";


	$result = mysql_query($query) or die("Couldn't execute query");

	$row = mysql_fetch_assoc($result);

	return $row;
}


/**
	 * creates photo
	 * @return bool (photos id)
	 */
function create_photo($params)
{
	$cxn = db_connect();

	//		 $query = sprintf("INSERT INTO photos set
	//			                             photos.title = '%s',
	//																	 photos.email = '%s',
	//																	 photos.discuss_url = '%s',
	//																	 photos.theme_id = '%s',
	//																	 photos.fullname = '%s',
	//																	 created_at = NOW()",
	//																	 mysql_real_escape_string($params['title']),
	//																	 mysql_real_escape_string($params['email']),
	//																	 mysql_real_escape_string($params['discuss_url']),
	//																	 mysql_real_escape_string($params['theme_id']),
	//																	 mysql_real_escape_string($params['fullname'])
	//																	 );


	$query = sprintf("INSERT INTO photos set
			                             photos.title = '%s',
																	 photos.theme_id = '%s',
																	 photos.fullname = '%s',
																	 created_at = NOW()",
	mysql_real_escape_string($params['title']),
	mysql_real_escape_string($params['theme_id']),
	mysql_real_escape_string($params['fullname'])
	);

	$result = mysql_query($query);

	if(!$result)
	{
		return false;
	}

	$photo_id = mysql_insert_id();

	create_photo_tags($params['tags'], $photo_id);

	return $photo_id;
}


/**
	 * creates our tags for our photos
	 * @param string $tag_str
	 * @param int $id
	 * @return bool
	 */
function create_photo_tags($tag_str, $id)
{
	$cxn = db_connect();

	$tags = explode(',', $tag_str);

	foreach($tags as $tag)
	{
		$not_empty = trim($tag);
		if(!empty($not_empty))
		{
			$query = sprintf("SELECT id from photos_tags WHERE name = '%s'",
			mysql_real_escape_string(strip_tags(trim($tag)))
			);
			$result = mysql_query($query);
			if(!$result)
			{
				false;
			}
			$num_rows = mysql_num_rows($result);

			if($num_rows > 0)
			{
				$tag_id = mysql_result($result, 0, 'id');
			}
			else
			{
				$query = sprintf("INSERT into photos_tags set name = '%s'",
				mysql_real_escape_string(strip_tags(trim($tag)))
				);
				$result = mysql_query($query);
				if(!$result)
				{
					false;
				}
				$tag_id = mysql_insert_id();
			}

			$query = sprintf("INSERT into photos_taggings set photo_id = '%s', tag_id = '%s'",
			mysql_real_escape_string($id),
			mysql_real_escape_string($tag_id)
			);
			$result = mysql_query($query);
		}
	}

	return true;

}


/**
	 * Finds photos by theme
	 * @param int $theme_id
	 * @return array
	 */
function find_photos_by_theme($theme_id)
{
	$cxn = db_connect();

	$query = sprintf("SELECT photos.id, photos.title, photos.theme_id, photos.created_at, photos.fullname, photos.vote_cache, photos.discuss_url, photos_themes.name as theme FROM photos, photos_themes  WHERE theme_id = '%s' and photos.theme_id = photos_themes.id  ORDER by vote_cache DESC ", mysql_real_escape_string($theme_id));

	$result = mysql_query($query);
	if(!$result)
	{
		return false;
	}

	$result = db_result_to_array($result);

	return $result;

}


/**
	 * Finds Themes
	 * @return array
	 */
function find_themes()
{
	$cxn = db_connect();

	$query = sprintf("SELECT * from photos_themes order by id DESC",
	mysql_real_escape_string($theme_id));

	$result = mysql_query($query);
	if(!$result)
	{
		return false;
	}

	$result = db_result_to_array($result);

	return $result;

}




/**
	 * Finds tags attached to a photo
	 * @return array
	 */
function photo_tags($id)
{
	$cxn = db_connect();

	$query = sprintf("SELECT
			                   photos_tags.id, name, COUNT(*) as count
			                FROM
											   photos_taggings, photos_tags
											WHERE
											   photos_tags.id = photos_taggings.tag_id and photo_id = '%s'
											GROUP by tag_id
														 ",
	mysql_real_escape_string($id));

	$result = mysql_query($query);
	if(!$result)
	{
		return false;
	}

	$result = db_result_to_array($result);

	return $result;

}


/**
	 * Finds tags attached to a photo
	 * @return array
	 */
function find_tags()
{
	$cxn = db_connect();

	$query = sprintf("SELECT photos_tags.id, name, COUNT(*) as count FROM photos_taggings, photos_tags WHERE photos_tags.id = photos_taggings.tag_id GROUP by tag_id");

	$result = mysql_query($query);
	if(!$result)
	{
		return false;
	}

	$result = db_result_to_array($result);

	return $result;

}



/**
	 * Finds photos by tag
	 * @param string $tag
	 * @return array
	 */
function find_photos_by_tag($tag)
{
	$cxn = db_connect();

	$query = sprintf("SELECT photos.id,
			                       photos.title,
														 photos.theme_id,
														 photos.created_at,
														 photos.fullname,
														 photos.vote_cache,
														  photos.discuss_url,
														 photos_themes.name as theme
										   FROM
											     photos, photos_themes, photos_taggings, photos_tags
											 WHERE
											     photos.theme_id = photos_themes.id and photos.id = photos_taggings.photo_id and
													 photos_taggings.tag_id = photos_tags.id and photos_tags.name = '%s'

													  ORDER by vote_cache DESC
											    ",
	mysql_real_escape_string($tag));

	$result = mysql_query($query);
	if(!$result)
	{
		return mysql_error();
	}

	$result = db_result_to_array($result);

	return $result;

}



/**
	 * Finds theme by id
	 * @return array
	 */
function find_theme($id)
{
	$cxn = db_connect();

	$query = sprintf("SELECT * FROM photos_themes
			                         WHERE id = '%s'
														 ",
	mysql_real_escape_string($id));

	$result = mysql_query($query);
	if(!$result)
	{
		false;
	}

	$row = mysql_fetch_array($result);

	return $row;

}


/**
	 * Finds theme by id
	 * @return array
	 */
function find_photo($id)
{
	$cxn = db_connect();

	$query = sprintf("SELECT
			                       photos.id,
			                       photos.title,
														 photos.theme_id,
														 photos.created_at,
														 photos.fullname,
														 photos.vote_cache,
														 photos.discuss_url,
														 photos_themes.name as theme

			                         FROM
			                           photos, photos_themes
			                         WHERE
															  photos.theme_id = photos_themes.id and
															  photos.id = '%s'
														 ",
	mysql_real_escape_string($id));

	$result = mysql_query($query);
	if(!$result)
	{
		false;
	}

	$row = mysql_fetch_array($result);

	return $row;

}



/**
	 * adds a vote to photo
	 * @param int $id
	 * @return bool
	 */
function photo_vote($id)
{
	$cxn = db_connect();

	$query = sprintf("SELECT id FROM photos_votes WHERE ip_address = '%s' and photo_id = '%s' ", mysql_real_escape_string($_SERVER['REMOTE_ADDR']), mysql_real_escape_string($id));

	$result = mysql_query($query);

	if(!$result)
	{
		echo mysql_error();
	}

	$num_row = mysql_num_rows($result);

	// looks like user has already voted.
	if($num_row > 0)
	{
		return false;
	}
	elseif(find_photo($id))
	{
		$query = sprintf("INSERT into photos_votes set photo_id = '%s', ip_address = '%s', created_at = NOW()", mysql_real_escape_string($id), mysql_real_escape_string($_SERVER['REMOTE_ADDR']));

		$result = mysql_query($query);

		$vote_id = mysql_insert_id();
		$vote_cache = vote_count($id);
		$query = sprintf("UPDATE photos set vote_cache = '%s' WHERE id = '%s'",
		mysql_real_escape_string($vote_cache),
		mysql_real_escape_string($id)
		);
		$result = mysql_query($query);

	}

	return $vote_id;
}


function checkIfAdmin(){

	$admins = array("Slater", "Mitch");

	if(isset($_SESSION['user'])){
		$user = $_SESSION['user'];

		foreach ($admins as $key => $value) {
			if ($value == $user->user_name) {
				return true;
			}
		}

		return false;
	}else{
		return false;
	}
}

function photo_delete($id){
	$cxn = db_connect();

	$query = sprintf("delete FROM photos_votes WHERE photo_id = '%s'", mysql_real_escape_string($id));
	$result = mysql_query($query);

	$query = sprintf("delete FROM photos_taggings WHERE photo_id = '%s'", mysql_real_escape_string($id));
	$result = mysql_query($query);

	$query = sprintf("delete FROM photos_comments WHERE photo_id = '%s'", mysql_real_escape_string($id));
	$result = mysql_query($query);

	$query = sprintf("delete FROM photos WHERE id = '%s'", mysql_real_escape_string($id));
	$result = mysql_query($query);
}



/**
		 * counts photo votes
		 * @param int $id
		 * @return int
		 */
function vote_count($id)
{
	$cxn = db_connect();
	$query = sprintf("SELECT count(*) as count FROM photos_votes WHERE photo_id = '%s'",
	mysql_real_escape_string($id)
	);
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	return $row['count'];
}


/**
	 * @return array
	 */
function theme_photo($theme_id)
{
	$cxn = db_connect();

	$query = sprintf("SELECT
			                       photos.id,
			                       photos.title,
														 photos.theme_id,
														 photos.created_at,
														 photos.fullname,
														 photos.vote_cache,
														 photos.discuss_url,
														 photos_themes.name as theme
			                         FROM
			                           photos, photos_themes
			                         WHERE
															  photos.theme_id = photos_themes.id and
															  photos.theme_id = '%s'
																ORDER by photos.vote_cache DESC LIMIT 1
														 ",
	mysql_real_escape_string($theme_id));

	$result = mysql_query($query);

	if(!$result)
	{
		false;
	}

	$row = mysql_fetch_array($result);

	return $row['id'];

}

/**
	 * Turns MYSQL resource into an array
	 * @param resource $result
	 * @return array
	 */
function db_result_to_array($result)
{
	$res_array = array();

	for ($count = 0; $row = mysql_fetch_array($result); $count++)
	{
		$res_array[$count] = $row;
	}

	return $res_array;

}


?>
